﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace HY.BLL.msg
{
  public  class msgbase
    {
      
        //发出 
        public static DataTable get_list(string us,string stype ,string whe,string nm)
        {
            string sqltxt = "select * from msg_list where stype=" + stype + " and sendnm='" + us + "'  ";

            if (stype=="1")  //收邮件
                sqltxt = "select * from msg_list where stype=" + stype + "  and sendnm<>'" + us + "' and outnm='" + nm + "'  ";

            if (stype=="3") //删除
                sqltxt = "select * from msg_list where stype=" + stype + " and sendnm='" + us + "' or outnm='" + nm + "'  ";
                        
            if (whe != "") sqltxt = sqltxt + " and " + whe;

            sqltxt = sqltxt + " order by sid desc ";

            DataTable dt = DAL.DbHelperSQL.SqlDataAdapter(sqltxt);
            
            return dt;

        }

        //发出 
        public static DataTable get_list_top(string us, string stype, string whe, string nm)
        {
            string sqltxt = "select top 20 * from msg_list where stype=" + stype + " and sendnm='" + us + "'  ";

            if (stype == "1")  //收邮件
                sqltxt = "select top 20  * from msg_list where stype=" + stype + "  and sendnm<>'" + us + "' and outnm='" + nm + "'  ";

            if (stype == "3") //删除
                sqltxt = "select top 20  * from msg_list where stype=" + stype + " and sendnm='" + us + "' or outnm='" + nm + "'  ";

            if (whe != "") sqltxt = sqltxt + " and " + whe;

            sqltxt = sqltxt + " order by sid desc ";

            DataTable dt = DAL.DbHelperSQL.SqlDataAdapter(sqltxt);

            return dt;

        }

        //public static DataTable get_list(string us, string stype,string nm)
        //{
        //    string sqltxt = "select * from msg_list where stype=" + stype + " and sendnm='" + us + "'   ";

        //    if (stype == "1")  //收邮件
        //        sqltxt = "select * from msg_list where stype=" + stype + " and outnm='" + nm + "'  ";

        //    sqltxt = sqltxt + " order by sid desc ";
        //    DataTable dt = DAL.DbHelperSQL.SqlDataAdapter(sqltxt);
        //    return dt;
        //}


        public static DataTable get_list(string sid)
        {
            string sqltxt = "select * from msg_list where sid=" + sid + "  ";
            sqltxt = sqltxt + " order by sid desc ";
            DataTable dt = DAL.DbHelperSQL.SqlDataAdapter(sqltxt);
            return dt;
        }

      /// <summary>
      /// 写入邮件
      /// </summary>
      /// <param name="owid"></param>
      /// <param name="snm"></param>
      /// <param name="onm"></param>
      /// <param name="title"></param>
      /// <param name="cont"></param>
      /// <param name="sfile"></param>
      /// <param name="stype">0-发送，1－接收，2－草稿，3－删除</param>
      /// <returns></returns>
        public static int ins_msg(string owid, string snm, string onm,string title,string cont,string sfile ,string stype)
        {
            string sqltxt = "insert into msg_list values(N'" + owid + "',N'" + snm + "',N'" + onm + "',N'" + title + "',N'" + cont + "',getdate(),0,N'" + sfile + "'," + stype + ")  ";

            return DAL.DbHelperSQL.ExecuteNonQuery(sqltxt);

        }

   

      //标记删除
        public static int DeleteList1(string sidlist)
        {
            string sqltxt = "update msg_list set stype=3 where sid in(" + sidlist + ")  ";

            return DAL.DbHelperSQL.ExecuteNonQuery(sqltxt);

        }

        /// <summary>
        /// 真实删除一条数据
        /// </summary>
        public static int DeleteList2(string sidlist)
        {
            string sqltxt = "delete msg_list where sid in(" + sidlist + ") ";
            return DAL.DbHelperSQL.ExecuteNonQuery(sqltxt);
        }

    }
}
